<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use Session;

class UserController extends Controller
{
    /**
     * 执行用户操作
     *
     */
     public function index(Request $request)
    {
        //获取请求参数
        $users = User::where(function($query) use ($request){
            //检测参数
            if(!empty($request->username)) {
                $query -> where('username','like','%'.$request->username.'%');
            }

            if(!empty($request->email)) {
                $query->where('email', $request->email);
            }

        })->orderBy('id','desc')->paginate($request->input('num',10));


        //读取用户的信息
        return view('admin.user.index', ['users'=>$users,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request, [
            'username' => 'required|regex:/^\w{8,18}$/',
            'password' => 'required|regex:/^\S{6,30}$/|same:repassword'
        ],[
            'username.required' => '用户名不能为空',
            'username.regex' => '用户名格式不正确',
            'password.required' =>'密码不能为空',
            'password.regex'=>'密码格式不正确',
            'password.same'=>'两次密码不一致'
        ]);

        //实例化模型对象
        $user = new User;
        $user->username = $request->username;
        $user->password = Hash::make($request->password);
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->sex = $request->sex;

        //上传头像
        if($request->hasFile('profile')) {
            $dirname = './Uploads/'.date('Ymd').'/';
            $suffix = $request->file('profile')->getClientOriginalExtension();
            $name = time().rand(100000,999999).'.'.$suffix;
            $request->file('profile')->move($dirname, $name);
            $user->profile = trim($dirname,'.').$name;
        }

        if($user->save()){
            return redirect('/user');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //读取用户的信息
        $user = User::findOrFail($id);

        //显示模板
        return view('admin.user.edit', ['user'=>$user]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //表单验证

        //读取内容
        $user = User::findOrFail($id);

        // 提取内容
        $user->username = $request->username;
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->sex = $request->sex;

        //表单验证
        $this->validate($request, [
            'username' => 'required|regex:/^\w{8,18}$/',
        ],[
            'username.required' => '用户名不能为空',
            'username.regex' => '用户名格式不正确'
        ]);

        //上传头像
        if($request->hasFile('profile')) {
            //上传文件夹的路径
            $dirname = './Uploads/'.date('Ymd').'/';
            //获取文件的后缀
            $suffix = $request->file('profile')->getClientOriginalExtension();
            //文件名
            $name = time().rand(100000,999999).'.'.$suffix;
            $request->file('profile')->move($dirname, $name);
            //存入对象中(数据表中)
            $user->profile = trim($dirname,'.').$name;
        }

        if($user->save()){
            return redirect('/user');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取数据
        $user = User::findOrFail($id);

        if($user->delete()){
            return back()->with('success','执行成功');
        }else{
            return back()->with('error','执行失败');
        }
    }

    public function flash()
    {
        Session::flash('name',10);
        // Session::put('name','xiaohigh');
    }

    public function session()
    {
        echo Session::get('name');
    }
}
